
class DonationController {

    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        if(!params.max) params.max = 10
        [ donationList: Donation.list( params ) ]
    }

    def show = {
        def donation = Donation.get( params.id )

        if(!donation) {
            flash.message = "Donation not found with id ${params.id}"
            redirect(action:list)
        } else {
			return [ donation : donation ]
		}
    }

    def delete = {
        def donation = Donation.get( params.id )
        if(donation) {
            donation.delete()
            flash.message = "Donation ${params.id} deleted"
            redirect( action : list )
        }
        else {
            flash.message = "Donation not found with id ${params.id}"
            redirect( action : list )
        }
    }

    def edit = {
        def donation = Donation.get( params.id )

        if(!donation) {
            flash.message = "Donation not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ donation : donation ]
        }
    }

    def update = {
        def donation = Donation.get( params.id )
        if(donation) {
            donation.properties = params
            if(!donation.hasErrors() && donation.save()) {
                flash.message = "Donation ${params.id} updated"
                redirect(action:show,id:donation.id)
            }
            else {
                render(view:'edit',model:[donation:donation])
            }
        }
        else {
            flash.message = "Donation not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def donation = new Donation()
        donation.properties = params
        return ['donation':donation]
    }

    def save = {
        def donation = new Donation(params)
        if(!donation.hasErrors() && donation.save()) {
            flash.message = "Donation ${donation.id} created"
            redirect(action:show,id:donation.id)
        }
        else {
            render(view:'create',model:[donation:donation])
        }
    }
}